<template>
    <div class="search-bar-wrapper">
      <div class="search-bar">
        <!-- 职位类型下拉 -->
        <div class="job-type-selector">
          <div class="selected-job-type" @click="toggleDropdown">
            <span>{{ selectedJobType }}</span>
            <i class="arrow-down"></i>
          </div>
          <div v-if="isDropdownVisible" class="job-type-dropdown">
            <div
              v-for="jobType in jobTypes"
              :key="jobType"
              class="dropdown-item"
              @click="selectJobType(jobType)"
            >
              {{ jobType }}
            </div>
          </div>
        </div>

        <!-- 分隔线 -->
        <div class="divider"></div>

        <!-- 搜索输入框 -->
        <input
          type="text"
          class="search-input"
          placeholder="搜索职位、公司"
          v-model="searchKeyword"
          @keyup.enter="handleSearch"
        />

        <!-- 地图图标链接 -->
        <router-link to="/map" class="map-icon-wrapper">
          <i class="map-icon"></i>
          <span>地图</span>
        </router-link>

        <!-- 搜索按钮 -->
        <button class="search-button" @click="handleSearch">
          <span>搜索</span>
          <i class="search-icon"></i>
        </button>
      </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

// 初始化路由
const router = useRouter()

// 搜索框相关数据
const searchKeyword = ref('')
const selectedJobType = ref('职位类型')
const isDropdownVisible = ref(false)
const jobTypes = ['前端开发', '后端开发', '全栈工程师', 'UI设计师', '产品经理']

// 滚动相关数据
const isScrolled = ref(false)
const isHidden = ref(false)
const lastScrollPosition = ref(0)

// 职位类型下拉菜单
const toggleDropdown = () => {
  isDropdownVisible.value = !isDropdownVisible.value
}

const selectJobType = (jobType) => {
  selectedJobType.value = jobType
  isDropdownVisible.value = false
  // 选择下拉框后立即跳转
  navigateToJobList()
}

// 搜索功能
const handleSearch = () => {
  navigateToJobList()
}

// 跳转到职位列表页并携带参数
const navigateToJobList = () => {
  // 构建搜索关键词，组合职位类型和输入内容
  let keyword = ''
  if (selectedJobType.value !== '职位类型') {
    keyword = selectedJobType.value
  }
  if (searchKeyword.value.trim()) {
    keyword = keyword ? `${keyword} ${searchKeyword.value.trim()}` : searchKeyword.value.trim()
  }
  
  // 跳转到职位列表页
  router.push({
    path: '/jobList',
    query: {
      keyword: keyword || ''
    }
  })
}

// 滚动处理函数
const handleScroll = () => {
  const currentScrollPosition = window.scrollY || window.pageYOffset
  
  if (currentScrollPosition > 50) {
    isScrolled.value = true
    
    if (currentScrollPosition > lastScrollPosition.value) {
      isHidden.value = true
    } else {
      isHidden.value = false
    }
  } else {
    isScrolled.value = false
    isHidden.value = false
  }
  
  lastScrollPosition.value = currentScrollPosition
}

// 组件挂 载时添加滚动监听
onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

// 组件卸载时移除滚动监听
onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.search-bar-wrapper {
  margin-top: 35px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.search-bar {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 1100px;
  height: 76px;
  background-color: #ffffff;
  border-radius: 20px;
  padding: 0 12px;
  box-shadow: 0 4px 20px rgba(212, 190, 190, 0.08);
  border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
}

.search-bar:hover {
  box-shadow: 0 6px 24px rgba(255, 255, 255, 0.12);
  border-color: #19c3ae;
}

/* 职位类型 */
.job-type-selector {
  position: relative;
  padding: 0 16px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  color: #333;
  height: 100%;
  display: flex;
  align-items: center;
}

.selected-job-type {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 100%;
}

.selected-job-type:hover {
  color: #19c3ae;
}

.arrow-down {
  border: solid currentColor;
  border-width: 0 2px 2px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(45deg);
  transition: transform 0.2s ease;
}

.job-type-selector:hover .arrow-down {
  transform: rotate(45deg) translateY(2px);
}

.job-type-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  padding: 8px 0;
  z-index: 10;
  width: 180px;
  margin-top: 4px;
  overflow: hidden;
  border: 1px solid #e0e0e0;
}

.dropdown-item {
  padding: 10px 20px;
  transition: all 0.2s ease;
  color: #555;
}

.dropdown-item:hover {
  background-color: #f8f8f8;
  color: #19c3ae;
}

/* 分隔线 */
.divider {
  width: 1px;
  height: 24px;
  background-color: rgba(0, 0, 0, 0.08);
  margin: 0 8px;
}

/* 搜索输入框 */
.search-input {
  flex-grow: 1;
  border: none;
  outline: none;
  font-size: 15px;
  color: #333;
  height: 100%;
  padding: 0 12px;
  background: transparent;
  border-left: 1px solid #e0e0e0;
  border-right: 1px solid #e0e0e0;
  margin: 0 8px;
}

.search-input:focus {
  border-color: #19c3ae;
}

.search-input::placeholder {
  color: #aaa;
  font-weight: 400;
}

/* 地图图标 */
.map-icon-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  padding: 0 16px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s ease;
  height: 100%;
}

.map-icon-wrapper:hover {
  color: #19c3ae;
}

.map-icon {
  width: 18px;
  height: 18px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='currentColor' viewBox='0 0 16 16'%3E%3Cpath d='M8 16s6-5.686 6-10A6 6 0 0 0 2 6c0 4.314 6 10 6 10zm0-7a3 3 0 1 1 0-6 3 3 0 0 1 0 6z'/%3E%3C/svg%3E");
  background-size: contain;
  background-repeat: no-repeat;
}

/* 搜索按钮 */
.search-button {
  display: flex;
  align-items: center;
  gap: 8px;
  background-color: #19c3ae;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  padding: 0 24px;
  height: 44px;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-left: auto;
}

.search-button:hover {
  background-color: #15a895;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(25, 195, 174, 0.3);
}

.search-icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z'/%3E%3C/svg%3E");
  background-size: contain;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-container {
    padding: 12px 0;
  }
  
  .search-container.scrolled {
    padding: 8px 0;
  }
  
  .search-bar {
    height: 60px;
    padding: 0 8px;
  }
  
  .job-type-selector {
    padding: 0 12px;
    font-size: 14px;
  }
  
  .search-button {
    padding: 0 16px;
    height: 40px;
    font-size: 14px;
  }
  
  .map-icon-wrapper span {
    display: none;
  }
}
</style>